<?php
namespace Admin\Controller;
use Think\Controller;
class RecommendController extends CommonController {
     public function lists(){
		$listinfo = M("recommend");
		$list = $listinfo->query("select * from zwp_listinfo");
		foreach($list as $k=>$v){
			$count = substr_count($v['pid'],",");
			$list[$k]['order']=$count;
			$list[$k]["name"]= $this->kong($count).$v['name'];
		}
		$this->assign("lists",$list);
		if($_REQUEST["name"]){
			$name = $_POST["name"];
			$recommend = M('recommend'); // 实例化User对象
			$numPerPage = empty($_REQUEST["numPerPage"])?2:$_REQUEST["numPerPage"];
			$count  = $recommend->where("name = '$name'")->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,$numPerPage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $recommend->where("name = '$name'")->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			//当前数据
			$this->assign("lists",$list);
			//总条数
			$this->assign("totalCount",$count);
			//每页显示多少条
			$this->assign("numPerPage",$numPerPage);
			//页标数字多少个
			$this->assign("pageNumShown",$Page->totalPages);
			//当前显示第几页
			$page = empty($_REQUEST["pageNum"])?1:$_REQUEST["pageNum"];
			$this->assign("currentPage",$page);
			$this->assign("name",$name);
		}else{
			$recommend = M('recommend'); // 实例化User对象
			$numPerPage = empty($_REQUEST["numPerPage"])?2:$_REQUEST["numPerPage"];
			$count  = $recommend->count();// 查询满足要求的总记录数
			$Page  = new \Think\Page($count,$numPerPage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show  = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $recommend->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			//当前数据
			$this->assign("lists",$list);
			//总条数
			$this->assign("totalCount",$count);
			//每页显示多少条
			$this->assign("numPerPage",$numPerPage);
			//页标数字多少个
			$this->assign("pageNumShown",$Page->totalPages);
			//当前显示第几页
			$page = empty($_REQUEST["pageNum"])?1:$_REQUEST["pageNum"];
			$this->assign("currentPage",$page);
		}
		$this->display();
	}
	public function kong($a){
		$str = "";
		for($i=0;$i<$a;$i++){
			$str .= "&nbsp;---|";
		}
		return $str;
	}
	public function add(){
		$recommend = M("Recommend");
		$shuju = $recommend->select();
		$this->assign("shuju",$shuju);
		$this->display();
	}
	public function addAction(){
		$sql = M("recommend")->add($_POST);
		//判断
		if($sql){
			$this->success("添加成功");
		}else{
			$this->error("添加失败");
		}
	}
	public function del(){
		//取得要删除的id
		$id = $_GET["uid"];
		$sql = M("recommend")->where("id=$id")->delete($_POST);
		if($sql){
			$this->success("删除成功");
		}
	}
	public function edit(){
		$id = $_GET["uid"];
		$sql = M("recommend")->where("id=$id")->find();//查询一条数据
		$this->assign("sql",$sql);
		$this->display();
	}
	public function editAction(){
		$info = M("recommend")->save($_POST); // 根据条件更新记录
		if($info){
			//成功
			$this->success("修改成功");
		}else{
			//失败
			$this->error("修改失败");
			
		}
	}
}